/**
 *	(C) 2014 Yuanjj
 *
 *	define queue class
 *
 *	histroy
 * 		2014-08-07	yuanjj	- create the file
 */

#ifndef _QUEUE_H_
#define _QUEUE_H_

#include "typedef.h"
#include "define.h"

/**
 *	the way to put data into queue
 */
enum
{
    IN_BEFORE,			// put the data at the head of the queue
    IN_AFTER			// put the data at the tail of the queue
};

/**
 *	queue class
 */
class cls_queue
{
public:
    cls_queue();
    ~cls_queue();

protected:
    pem_s8_t			*m_queue;			// queue pointer
    pem_u32_t			m_head;				// the queue head pointer
    pem_u32_t			m_tail;				// the queue tail pointer
    pem_u32_t			m_leftsize;			// the left queue size
    pem_u32_t			m_maxsize;			// the max queue size
    MUTEX_LOCK			m_lock;				// queue lock

public:
    /**
     *	initialize the queue object
     */
    pem_error_t		instance( pem_u32_t	size );

    /**
     *	release the queue
     */
    pem_void_t		release();

    /**
     *	check the queue instance status
     */
    pem_bool_t		is_instance( );

    /**
     *	put data into the queue
     */
    pem_error_t		putdata( pem_s8_t* pdata, pem_u32_t size, pem_s32_t flag = IN_AFTER );

    /**
     *	get data from the queue
     */
    pem_error_t		getdata( pem_s8_t* pdata, pem_u32_t size );

    /**
     *	clear up the queue
     */
    pem_error_t		setempty();

protected:
    /**
     *	initialize the queue object
     */
    pem_error_t		_instance( pem_u32_t	size );

    /**
     *	put data into the queue
     */
    pem_error_t		_putdata( pem_s8_t* pdata, pem_u32_t size, pem_s32_t flag = IN_AFTER );

    /**
     *	put data at the tail of the queue
     */
    pem_void_t		__putdata_after( pem_s8_t* pdata, pem_u32_t size );

    /**
     *	put data at the head of the queue
     */
    pem_void_t		__putdata_before( pem_s8_t* pdata, pem_u32_t size );

    /**
     *	get data from the queue
     */
    pem_error_t		_getdata( pem_s8_t* pdata, pem_u32_t size );

    /**
     *	get data from the queue
     */
    pem_void_t		__getdata( pem_s8_t* pdata, pem_u32_t size );
};

#endif // _QUEUE_H_
